﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HugeFlow.CommandPattern
{
    public class InstantCommand : ICommand
    {
        Action _executeAction;
        Func<bool> _canExecuteAction;

        public InstantCommand(Action executeAction,
          Func<bool> canExecuteAction)
        {
            this._executeAction = executeAction;
            this._canExecuteAction = canExecuteAction;
        }

        public InstantCommand(Action executeAction)
            : this(executeAction, null)
        {
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecuteAction == null)
                return true;

            return (_canExecuteAction());
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _executeAction();
        }
    }

    public class InstantCommand<T> : ICommand
    {
        Action<T> _executeAction;
        Func<T, bool> _canExecuteAction;

        public InstantCommand(Action<T> executeAction,
          Func<T, bool> canExecuteAction)
        {
            this._executeAction = executeAction;
            this._canExecuteAction = canExecuteAction;
        }

        public InstantCommand(Action<T> executeAction)
            : this(executeAction, null)
        {
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecuteAction == null)
                return true;

            return (_canExecuteAction((T)parameter));
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _executeAction((T)parameter);
        }

    }
}
